import request from '@/utils/request'
import type { ApiResponse, PageResponse } from '@/types/api'

// 静态文件信息
export interface StaticFile {
  name: string
  path: string
  url: string
  size: string
  type: string
  lastModified: string
  siteId?: number
}

// 静态文件统计信息
export interface StaticFileStatistics {
  totalFiles: number
  totalSize: string
  htmlFiles: number
  xmlFiles: number
  otherFiles: number
  lastGenerated: string
  fileTypeStats: Record<string, number>
  sizeByType: Record<string, string>
}

// 静态文件查询参数
export interface StaticFileQueryParams {
  page?: number
  size?: number
  siteId?: number
  fileType?: string
  fileName?: string
  startDate?: string
  endDate?: string
}

// 静态文件管理API
export const staticFileApi = {
  // 获取静态文件列表
  getFileList(params: StaticFileQueryParams): Promise<ApiResponse<PageResponse<StaticFile>>> {
    return request({
      url: '/cms/admin/static-file/list',
      method: 'get',
      params
    })
  },

  // 获取静态文件统计信息
  getStatistics(siteId?: number): Promise<ApiResponse<StaticFileStatistics>> {
    return request({
      url: '/cms/admin/static-file/statistics',
      method: 'get',
      params: { siteId }
    })
  },

  // 获取文件内容
  getFileContent(filePath: string): Promise<ApiResponse<string>> {
    return request({
      url: '/cms/admin/static-file/content',
      method: 'get',
      params: { filePath }
    })
  },

  // 删除静态文件
  deleteFile(filePath: string): Promise<ApiResponse<void>> {
    return request({
      url: '/cms/admin/static-file/delete',
      method: 'delete',
      data: { filePath }
    })
  },

  // 批量删除静态文件
  deleteFiles(filePaths: string[]): Promise<ApiResponse<void>> {
    return request({
      url: '/cms/admin/static-file/batch-delete',
      method: 'delete',
      data: { filePaths }
    })
  },

  // 清空站点静态文件
  clearSiteFiles(siteId: number): Promise<ApiResponse<void>> {
    return request({
      url: `/cms/admin/static-file/clear/${siteId}`,
      method: 'delete'
    })
  },

  // 导出统计报告
  exportReport(siteId?: number): Promise<Blob> {
    return request({
      url: '/cms/admin/static-file/export-report',
      method: 'get',
      params: { siteId },
      responseType: 'blob'
    })
  },

  // 检查文件是否存在
  checkFileExists(filePath: string): Promise<ApiResponse<boolean>> {
    return request({
      url: '/cms/admin/static-file/exists',
      method: 'get',
      params: { filePath }
    })
  },

  // 获取文件详细信息
  getFileInfo(filePath: string): Promise<ApiResponse<StaticFile>> {
    return request({
      url: '/cms/admin/static-file/info',
      method: 'get',
      params: { filePath }
    })
  },

  // 搜索文件
  searchFiles(keyword: string, siteId?: number): Promise<ApiResponse<StaticFile[]>> {
    return request({
      url: '/cms/admin/static-file/search',
      method: 'get',
      params: { keyword, siteId }
    })
  },

  // 获取文件树结构
  getFileTree(siteId?: number): Promise<ApiResponse<any[]>> {
    return request({
      url: '/cms/admin/static-file/tree',
      method: 'get',
      params: { siteId }
    })
  },

  // 压缩文件
  compressFiles(filePaths: string[], outputPath: string): Promise<ApiResponse<string>> {
    return request({
      url: '/cms/admin/static-file/compress',
      method: 'post',
      data: { filePaths, outputPath }
    })
  },

  // 验证文件完整性
  validateFiles(siteId?: number): Promise<ApiResponse<any>> {
    return request({
      url: '/cms/admin/static-file/validate',
      method: 'post',
      data: { siteId }
    })
  },

  // 同步文件状态
  syncFileStatus(siteId?: number): Promise<ApiResponse<void>> {
    return request({
      url: '/cms/admin/static-file/sync',
      method: 'post',
      data: { siteId }
    })
  },

  // 获取磁盘使用情况
  getDiskUsage(): Promise<ApiResponse<any>> {
    return request({
      url: '/cms/admin/static-file/disk-usage',
      method: 'get'
    })
  },

  // 清理过期文件
  cleanupExpiredFiles(days: number): Promise<ApiResponse<any>> {
    return request({
      url: '/cms/admin/static-file/cleanup',
      method: 'post',
      data: { days }
    })
  },

  // 备份静态文件
  backupFiles(siteId: number, backupName: string): Promise<ApiResponse<string>> {
    return request({
      url: '/cms/admin/static-file/backup',
      method: 'post',
      data: { siteId, backupName }
    })
  },

  // 恢复静态文件
  restoreFiles(backupPath: string, siteId: number): Promise<ApiResponse<void>> {
    return request({
      url: '/cms/admin/static-file/restore',
      method: 'post',
      data: { backupPath, siteId }
    })
  },

  // 获取备份列表
  getBackupList(): Promise<ApiResponse<any[]>> {
    return request({
      url: '/cms/admin/static-file/backups',
      method: 'get'
    })
  },

  // 预览文件URL
  getPreviewUrl(filePath: string): string {
    return `/cms/admin/static-file/preview?filePath=${encodeURIComponent(filePath)}`
  },

  // 下载文件URL
  getDownloadUrl(filePath: string): string {
    return `/cms/admin/static-file/download?filePath=${encodeURIComponent(filePath)}`
  }
}

export default staticFileApi
